<?php
/**充值管理
 * Created by leiying
 * Date: 2018-02-25
 * Time: 11:43:39
 */

namespace Admin\Controller;


class UserAccountLogController extends AdminController
{
    /**列表
     * @author leiying
     */
    public function UserAccountLogList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_UserAccountLogList',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_UserAccountLogList',null);
            }
            $get = $search_data = session('searchdata_UserAccountLogList');
        }
        //搜索条件：用户ID
        if(isset($get['user_id']) && $get['user_id'] != ''){
            $search_data['user_id'] = trim($get['user_id']);
            $where['user_id'] = array('like','%'.$search_data['user_id'].'%');
        }
        $count = M('user_account_log')->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','log_id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString =$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);

        $lists = M('user_account_log')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();
        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);
        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }


    /**批量操作
     * @param null $method
     * @author leiying
     */
    public function changeStatus($method=null){
        $id = I('post.id');
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        switch ( strtolower($method) ){
            case 'deletegroup':
                $res = M("user_account_log")->where(array("log_id"=>array('in',$id)))->delete();
                if($res){
                    $this->success("删除成功");
                }else{
                    $this->error("删除失败");
                }
                break;
            default:
                $this->error($method.'参数非法');
        }
    }

    /**添加
     * @author leiying
     */
    public function UserAccountLogAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            /*处理参数(请根据业务逻辑适当修改)*/
            //用户ID
            $user_id = trim($post['user_id']);
            if(empty($user_id)){
                $this->error("用户ID不能为空");
            }
            //账号金额
            $user_balance = trim($post['user_balance']);
            if(empty($user_balance)){
                $this->error("账号金额不能为空");
            }
            //投资金额
            $user_investment = trim($post['user_investment']);
            if(empty($user_investment)){
                $this->error("投资金额不能为空");
            }
            //积分
            $user_integral = trim($post['user_integral']);
            if(empty($user_integral)){
                $this->error("积分不能为空");
            }
            //记录时间
            $change_time = trim($post['change_time']);
            if(empty($change_time)){
                $this->error("记录时间不能为空");
            }
            //明细描述
            $change_desc = trim($post['change_desc']);
            if(empty($change_desc)){
                $this->error("明细描述不能为空");
            }
            //1-余额充值  2-余额提现  3-投资金转入 4-投资金转出5-积分变动 99-平台修正
            $change_action = trim($post['change_action']);
            if(empty($change_action)){
                $this->error("1-余额充值  2-余额提现  3-投资金转入 4-投资金转出5-积分变动 99-平台修正不能为空");
            }
            //2增加1支出
            $change_type = trim($post['change_type']);
            if(empty($change_type)){
                $this->error("2增加1支出不能为空");
            }
            $data = array(
                'add_time'=>time(),
                'user_id'=>$user_id,
                'user_balance'=>$user_balance,
                'user_investment'=>$user_investment,
                'user_integral'=>$user_integral,
                'change_time'=>$change_time,
                'change_desc'=>$change_desc,
                'change_action'=>$change_action,
                'change_type'=>$change_type,
            );
            $res = M('user_account_log')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }

    /**编辑
     * @author leiying
     */
    public function UserAccountLogEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            /*处理参数(请根据业务逻辑适当修改)*/
            //用户ID
            $user_id = trim($post['user_id']);
            if(empty($user_id)){
                $this->error("用户ID不能为空");
            }
            //账号金额
            $user_balance = trim($post['user_balance']);
            if(empty($user_balance)){
                $this->error("账号金额不能为空");
            }
            //投资金额
            $user_investment = trim($post['user_investment']);
            if(empty($user_investment)){
                $this->error("投资金额不能为空");
            }
            //积分
            $user_integral = trim($post['user_integral']);
            if(empty($user_integral)){
                $this->error("积分不能为空");
            }
            //记录时间
            $change_time = trim($post['change_time']);
            if(empty($change_time)){
                $this->error("记录时间不能为空");
            }
            //明细描述
            $change_desc = trim($post['change_desc']);
            if(empty($change_desc)){
                $this->error("明细描述不能为空");
            }
            //1-余额充值  2-余额提现  3-投资金转入 4-投资金转出5-积分变动 99-平台修正
            $change_action = trim($post['change_action']);
            if(empty($change_action)){
                $this->error("1-余额充值  2-余额提现  3-投资金转入 4-投资金转出5-积分变动 99-平台修正不能为空");
            }
            //2增加1支出
            $change_type = trim($post['change_type']);
            if(empty($change_type)){
                $this->error("2增加1支出不能为空");
            }
            $data = array(
                'add_time'=>1519530219,
                'user_id'=>$user_id,
                'user_balance'=>$user_balance,
                'user_investment'=>$user_investment,
                'user_integral'=>$user_integral,
                'change_time'=>$change_time,
                'change_desc'=>$change_desc,
                'change_action'=>$change_action,
                'change_type'=>$change_type,
            );
            $res = M('user_account_log')->where(array("log_id"=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('user_account_log')->where(array('log_id'=>$id))->find();
            $this->assign('res',$res);
            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

}